package com.yenlien.traceyun.plant.service;

import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import com.yenlien.traceyun.plant.dao.ColdCategoryDao;
import com.yenlien.traceyun.plant.entity.ColdCategory;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2022/9/28
 * @Author jiaLong
 * @Descript
 */

@Service
public class ColdCategoryService {

    @Autowired
    private ColdCategoryDao coldCategoryDao;

    @Autowired
    private FileRestService fileRestService;


    public ColdCategory save(ColdCategory coldCategory) {
        if(StringUtils.isBlank(coldCategory.getId())){
            if (StringUtils.isNotBlank(coldCategoryDao.checkByLabel(coldCategory.getLabel())))
                throw new BusinessException("该名字已存在");
        }
        return coldCategoryDao.save(coldCategory);
    }

    public void delById(String id){
        coldCategoryDao.deleteById(id);
    }


    public List<ColdCategory> loadById(String parentId, String type) {
        List<ColdCategory> coldCategoryList;
        if(StringUtils.isBlank(type)){
            coldCategoryList = coldCategoryDao.findAll();
        }else {
            coldCategoryList = coldCategoryDao.findByType(type);
        }
        List<ColdCategory> coldCategoryTree = new ArrayList<>();
        for (ColdCategory coldCategory : coldCategoryList) {
            if (StringUtils.isBlank(parentId) && StringUtils.isBlank(coldCategory.getParentId())) {
                coldCategoryTree.add(new ColdCategory(coldCategory.getId(), coldCategory.getLabel(), coldCategory.getParentId(),
                        coldCategory.getDisplayImg(), tree(coldCategoryList, coldCategory.getId())));
            }
//            if (StringUtils.isNotBlank(parentId) && parentId.equals(coldCategory.getId())) {          // 包含自己
            if (StringUtils.isNotBlank(parentId) && parentId.equals(coldCategory.getParentId())) {      // 不包含自己
                coldCategoryTree.add(new ColdCategory(coldCategory.getId(), coldCategory.getLabel(), coldCategory.getParentId(),
                        coldCategory.getDisplayImg(), tree(coldCategoryList, coldCategory.getId())));
            }
        }
        return coldCategoryTree;
    }


    public List<ColdCategory> loadAllParent(String type) {
        return coldCategoryDao.loadAllParent(type);
    }


    // 封装树形结构
    public static List<ColdCategory> tree(List<ColdCategory> coldCategoryList, String parentId) {
        List<ColdCategory> childList = new ArrayList<>();
        for (ColdCategory coldCategory : coldCategoryList) {
            if (parentId.equals(coldCategory.getParentId())) {
                childList.add(new ColdCategory(coldCategory.getId(), coldCategory.getLabel(), coldCategory.getParentId(),
                         coldCategory.getDisplayImg(), tree(coldCategoryList, coldCategory.getId())));
            }
        }
        return childList;
    }


}
